package com.cian.controller;


import com.cian.entity.Account;
import com.cian.entity.AccountInformation;
import com.cian.entity.dto.AccountDTO;
import com.cian.mapper.AccountInformationMapper;
import com.cian.mapper.AccountMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Cian
 * @since 2021-02-26
 */
@RestController
@RequestMapping("//account")
public class AccountController {
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    AccountInformationMapper accountInformationMapper;

    @RequestMapping("/login")
    public Boolean login(@RequestParam("account")String account,
                         @RequestParam("password")String password){

        return true;
    }

    @RequestMapping("/register")
    public Boolean register(@RequestParam("account")String account,
                            @RequestParam("password")String password){
        if (accountMapper.selectByName(account) != null){
            return false;
        }
        Account user = new Account();
        user.setUsername(account);
        user.setPassword(password);
        accountMapper.insert(user);
        // 插入用户列表，查看用户对应的id
        user = accountMapper.selectByNameAndPassword(account, password);
        // 根据用户对应的id，传入用户详细信息
        AccountInformation accountInformation = new AccountInformation();
        accountInformation.setAccountId(user.getAccountId());
        accountInformationMapper.insert(accountInformation);
        return true;
    }

    @RequestMapping("/obtain/init")
    public AccountDTO obtainAccountDTO(@RequestParam("account")String account,
                                       @RequestParam("password")String password){
        Account user = accountMapper.selectByNameAndPassword(account, password);
        if(user == null) return null;
        return new AccountDTO(
                user,
                accountInformationMapper.selectById(user.getAccountId())
        );
    }

    @RequestMapping("/modify")
    public Boolean modifyInformation(@RequestParam("account")String account,
                                     @RequestParam("password")String password,
                                     @RequestParam("licenseKey")String licenseKey,
                                     @RequestParam("longitude")double longitude,
                                     @RequestParam("latitude")double latitude,
                                     @RequestParam("detail")String detail){
        Account user = accountMapper.selectByName(account);
        // 判断是否有修改密码
        if(!user.getPassword().equals(password) || !user.getLicenseKey().equals(licenseKey)){
            user.setPassword(password);
            user.setLicenseKey(licenseKey);
            accountMapper.updateById(user);
        }
        // 修改用户类
        AccountInformation accountInformation = new AccountInformation();
        accountInformation.setAccountId(user.getAccountId());
        accountInformation.setMLatitude(latitude);
        accountInformation.setMLongitude(longitude);
        accountInformation.setMDetail(detail);
        accountInformationMapper.updateById(accountInformation);
        return true;
    }

    /**
     * <p>
     *     判断子服务器的连接对应的账户是否正确
     * </p>
     * @return 验证的结果
     */
    @RequestMapping("/obtain/license")
    public Boolean judgeLicenseKey(@RequestParam("username") String username,
                                   @RequestParam("licenseKey") String license){
        Integer id = accountMapper.selectByNameAndLicense(username, license);
        return (id == null);
    }


}

